/***************************************************************************
 *   oboe.controllers.jml.helpers.scrollBar.Bar                            *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.controllers.jml.helpers.scrollBar
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	
	import oboe.utils.CallbackHandle;
	
	public class Bar extends Sprite
	{
		public static const CSIZE:int = 20;
		
		public var btnFillColor1:uint = 0x909090;
		public var btnFillColor2:uint = 0x888888;
		public var barFillColor:uint = 0x707070;
		public var btnDragColor1:uint = 0xB0B0B0;
		public var btnDragColor2:uint = 0xA0A0A0;
		
		protected var gradMat:Matrix;
		
		protected var container:DisplayObjectContainer;
		public var offset:Number = 0;
		
		private var dragCB:CallbackHandle;
		private var dragCB2:CallbackHandle;
		
		protected var isHidden:Boolean;
		protected var isSmall:Boolean;
		
		public function Bar( container:DisplayObjectContainer )
		{
			super();
			this.container = container;
			
			this.gradMat = new Matrix();
			
			Object( container ).addCallback( "changeSize", update );
			Object( this ).addCallback( MouseEvent.MOUSE_DOWN, onStartDrag );
			
			container.addEventListener( "showBar", onShowBar );
			container.addEventListener( "hideBar", onHideBar );
			Object( container ).addCallbackEx( "onScroll", onScroll ); 
			
			isHidden = true;
			isSmall = false;
			
			this.update();
		}
		
		private function onShowBar( e:BarEvent ):void
		{
			if( e.bar == this || this.isSmall ) return;
			this.isSmall = true;
			this.update();
		}

		private function onHideBar( e:Event ):void
		{
			if( e.currentTarget == this || !this.isSmall ) return;
			this.isSmall = false;
			this.update();
		}
		
		
		private function onStartDrag():void
		{
			if( this.dragCB ) Object( this ).removeCallback( this.dragCB );
			this.dragCB = Object( this.stage ).addCallbackEx( MouseEvent.MOUSE_MOVE, onPreDrag );
			this.dragCB2 = Object( this.stage ).addCallback( MouseEvent.MOUSE_UP, onStopDrag );

			this.onDrag();
			this.emit("onScroll");
		}
		
		protected function emit(e:String, sendThis:Boolean=true ):void
		{
			this.container.dispatchEvent( new BarEvent(e, sendThis?this:null ) );
		}
		
		private function onScroll( e:BarEvent ):void
		{
			this.update( e.bar == this );
		}
		
		private function onPreDrag( e:MouseEvent ):void
		{
			if( !e.buttonDown )
			{
				this.onStopDrag();
			}
			else
			{
				this.onDrag();
				this.emit("onScroll");
			}
		}
		
		private function onStopDrag():void
		{
			if( this.dragCB ) Object( this.stage ).removeCallback( this.dragCB );
			if( this.dragCB2 ) Object( this.stage ).removeCallback( this.dragCB2 );
			this.emit("onScroll", false);
		}
		
		protected function onDrag():void{}
		
		public function update( dragging:Boolean=false ):void{} // override me
		
	}
}